/*
 * Copyright (C) 2004 Derek James and Philip Tucker
 * 
 * This file is part of ANJI (Another NEAT Java Implementation).
 * 
 * ANJI is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * created by Derek James
 */
package com.anji.dilemma;

import com.anji.integration.Activator;
import com.anji.tournament.Player;

/**
 * Prisoner's dilemma player whose moves are determined by a 1x1 neural network.
 * @author Philip Tucker
 */
public class NeuralNetPlayer extends MemoryDilemmaPlayer {

  private Activator activator = null;

  /**
   * Create subject whose "brains" are <code>anActivator</code>, 1 input and 1
   * output.
   * @param anActivator
   */
  public NeuralNetPlayer(Activator anActivator) {
    if (anActivator.getInputDimension() != 1) {
      throw new IllegalArgumentException("wrong input dimension: "
          + anActivator.getInputDimension());
    }
    if (anActivator.getOutputDimension() != 1) {
      throw new IllegalArgumentException("wrong output dimension: "
          + anActivator.getOutputDimension());
    }
    this.activator = anActivator;
  }

  /**
   * @see Player#getPlayerId()
   */
  public String getPlayerId() {
    return activator.getName();
  }

  /**
   * @see com.anji.tournament.Player#reset()
   */
  public void reset() {
    activator.reset();
  }

  /**
   * @see java.lang.Object#toString()
   */
  public String toString() {
    return activator.getName();
  }

  public Choice chooseImpl(Choice opponentPreviousChoice) {
    double[] stimulus = new double[1];
    if (opponentPreviousChoice == Choice.DEFECT) {
      stimulus[0] = -1;
    } else if (opponentPreviousChoice == Choice.COOPERATE) {
      stimulus[0] = 1;
    } else {
      // First move.
      stimulus[0] = 0;
    }
    double[] response = activator.next(stimulus);
    if (response.length != 1) {
      throw new IllegalStateException("wrong dimension of response: "
          + response.length);
    }
    return response[0] < 0 ? Choice.DEFECT : Choice.COOPERATE;
  }

}
